﻿/*
 * Copyright 2008 Pimm Hogeling, Karim Sempel and Demian Sempel
 * 
 * This file is part of 8bitplays.
 * 
 * 8bitplays is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 * 
 * 8bitplays is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with 8bitplays. If not, see <http://www.gnu.org/licenses/>.
 */
package nl.sandmen.team.data {
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import nl.sandmen.team.events.AlbumEvent;
	/**
	 * @author Team Sandmen
	 */
	public class Album extends EventDispatcher {
		public var artist:String;
		public var cover:BitmapData;
		private var coverLoader:Loader;
		public var coverProgress:Number;
		private var id:uint;
		public var name:String;
		[Embed(source="../../../../../media/NoCover.png")]
		private static const NO_COVER_ASSET:Class;
		private static var noCover:BitmapData;
		public var trackList:Array;
		public function Album(artist:String, name:String, id:uint, trackList:Array):void {
			this.artist = artist;
			this.name = name;
			this.id = id;
			this.trackList = trackList;
		}
		private function defautCover():void {
			if (noCover) {
				cover = noCover;
				dispatchEvent(new AlbumEvent(AlbumEvent.COVER_LOADED, cover, this));
				return;
			}
			noCover = new BitmapData(0x1CD, 0x0F6, false, 0xFF222222);
			noCover.draw(new NO_COVER_ASSET());
			cover = noCover;
			dispatchEvent(new AlbumEvent(AlbumEvent.COVER_LOADED, cover, this));
		}
		public function getCover(urlRequestHeaders:Array = null, loaderContext:LoaderContext = null):void {
			if (cover) {
				dispatchEvent(new AlbumEvent(AlbumEvent.COVER_LOADED, cover, this));
			} else {
				coverLoader = new Loader();
				coverLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderComplete);
				coverLoader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, loaderHttpStatus);
				var idString:String = id.toString();
				while (idString.length !== 3) {
					idString = "0" + idString;
				}
				var request:URLRequest = new URLRequest("http://www.8bitpeoples.com/images/albums/large/8BP" + idString + ".gif");
				if (urlRequestHeaders) {
					request.requestHeaders = urlRequestHeaders;
				}
				coverLoader.load(request, loaderContext);
			}
		}
		private function loaderComplete(e:Event):void {
			coverLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loaderComplete);
			coverLoader.contentLoaderInfo.removeEventListener(HTTPStatusEvent.HTTP_STATUS, loaderHttpStatus);
			// Check the width and height of the cover.
			if ((coverLoader.content.width & 0x1FC) === 0x1CC && coverLoader.content.height === 0x0F6) {
				cover = new BitmapData(0x1CD, 0x0F6, false, 0xFF222222);
				cover.draw(coverLoader);
				dispatchEvent(new AlbumEvent(AlbumEvent.COVER_LOADED, cover, this));
			} else {
				defautCover();
			}
			coverLoader.unload();
			coverLoader = null;
		}
		private function loaderHttpStatus(e:HTTPStatusEvent):void {
			// Get the default cover if the server returns a HTTP status that starts with 4 or higher (4 is client error, 5 is
			//  server error).
			if (parseInt(e.status.toString().substr(0, 1)) > 3) {
				coverLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loaderComplete);
				coverLoader.contentLoaderInfo.removeEventListener(HTTPStatusEvent.HTTP_STATUS, loaderHttpStatus);
				coverLoader.close();
				coverLoader = null;
				defautCover();
			}
		}
	}
}